package miku.firstgame.world
{
	import com.saia.starlingPunk.SP;
	import com.saia.starlingPunk.SPWorld;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.ScrollText;
	import feathers.controls.TextInput;
	import feathers.core.PopUpManager;
	import feathers.layout.TiledRowsLayout;
	
	import miku.firstgame.event.RoomEvent;
	import miku.firstgame.model.Me;
	import miku.firstgame.model.proxy.ProxyHelper;
	import miku.firstgame.net.pack.AddRoomRequestPack;
	import miku.firstgame.ui.component.CreateRoomPanel;
	import miku.firstgame.ui.component.MessageOutput;
	import miku.firstgame.ui.component.MessagePanel;
	import miku.firstgame.ui.control.ListEx;
	import miku.firstgame.view.RoomWorldMediator;
	
	import org.puremvc.as3.patterns.facade.Facade;
	
	import starling.events.Event;

	public class RoomWorld extends SPWorld
	{
		private var nameInfo:Label;
		private var nameLabel:Label;

		private var roomInfo:Label;
		public var roomList:ListEx;
		private var createRoomButton:Button;
		private var createRoomPanel:CreateRoomPanel;

		public var userListLabel:Label;
		public var userList:ListEx;

//		public var messageOutput:MessageOutput;
//		public var messageInput:TextInput;
//		public var messageSendButton:Button;
		public var messagePanel:MessagePanel;

		public function RoomWorld()
		{
			super();
		}

		override public function begin():void
		{
			super.begin();
			initUI();
			Facade.getInstance().registerMediator(new RoomWorldMediator(this));
		}

		override public function end():void
		{
			super.end();
			removeAll();
			removeChildren(0, -1, true);
			Facade.getInstance().removeMediator(RoomWorldMediator.NAME);
		}

		private function initUI():void
		{
			nameInfo = new Label();
			nameInfo.text = "你的用户名:";
			addChild(nameInfo);

			nameLabel = new Label();
			nameLabel.text = Me.Instace.name;
			addChild(nameLabel);

			roomInfo = new Label();
			roomInfo.text = "房间列表";
			addChild(roomInfo);

			roomList = new ListEx();
			roomList.setSize(500, 290);
			roomList.itemRendererProperties.labelField = "roomName";
			roomList.itemRendererProperties.accessoryLabelField = "createUName";
			addChild(roomList);

			var layout:TiledRowsLayout = new TiledRowsLayout();
			layout.gap = 10;
			layout.paddingTop = layout.paddingRight = layout.paddingBottom = layout.paddingLeft = 15;
			layout.useVirtualLayout = false;
			layout.typicalItemHeight = 80;
			layout.useSquareTiles = false;
			layout.typicalItemWidth = 150;
			layout.tileHorizontalAlign = TiledRowsLayout.TILE_HORIZONTAL_ALIGN_JUSTIFY;
			layout.tileVerticalAlign = TiledRowsLayout.TILE_VERTICAL_ALIGN_JUSTIFY;

			roomList.layout = layout;

			createRoomButton = new Button();
			createRoomButton.label = "创建房间";
			addChild(createRoomButton);
			createRoomButton.addEventListener(Event.TRIGGERED, onCreateButtonClick);

			userList = new ListEx();
			userList.setSize(200, 600);
			userList.itemRendererProperties.labelField = "name";
			addChild(userList);

//			var layout:HorizontalLayout = new HorizontalLayout();
//			layout.verticalAlign = HorizontalLayout.VERTICAL_ALIGN_JUSTIFY;
//			layout.gap = 10;
//			layout.paddingTop = layout.paddingRight = layout.paddingBottom =
//				layout.paddingLeft = 15;

			userListLabel = new Label();
			userListLabel.text = "用户列表:";
			addChild(userListLabel);

			messagePanel = new MessagePanel();
			messagePanel.setSize(500, 210);
			addChild(messagePanel);

			resize();
		}

		private function onCreateButtonClick(e:Event):void
		{
			if (createRoomPanel == null)
			{
				createRoomPanel = new CreateRoomPanel();
				createRoomPanel.setSize(400, 400);
				createRoomPanel.addEventListener(RoomEvent.ROOM_CREATE, onRoomCreate);
				createRoomPanel.addEventListener(RoomEvent.ROOM_CANCEL, onRoomCancel);
			}
			PopUpManager.addPopUp(createRoomPanel);
		}

		private function onRoomCreate(e:RoomEvent):void
		{
			createRoomPanel.showInfo("创建房间中...");
			createRoomPanel.isEnabled = false;
			//TODO need to send to server
			//<ar n="" p=""/>
			//暂时放到这里，需要移走 
			
			var pack:AddRoomRequestPack = new AddRoomRequestPack();
			pack.name = e.roomName;
			pack.password = "";
			ProxyHelper.send(pack);
		}

		private function onRoomCancel(e:RoomEvent):void
		{
			PopUpManager.removePopUp(createRoomPanel);
		}

		public function notifyRoomCreate(value:Boolean):void
		{
			createRoomPanel.isEnabled = true;
			if (value)
			{
				//TODO 创建房间成功并进入房间
				createRoomPanel.showInfo("房间创建成功");
				PopUpManager.removePopUp(createRoomPanel);
			}
			else
			{
				createRoomPanel.showInfo("房间创建失败");
			}
		}

		public function resize():void
		{
			nameInfo.x = 30;
			nameInfo.y = 30;

			nameLabel.x = 120;
			nameLabel.y = 30;

			roomInfo.x = 30;
			roomInfo.y = 65;

			roomList.x = roomInfo.x;
			roomList.y = roomInfo.y + 20;

			createRoomButton.y = 55;
			createRoomButton.x = 100;

			userList.x = SP.width - userList.width - 10;
			userList.y = 50;

			userListLabel.x = userList.x;
			userListLabel.y = 20;

//			messageInput.x = 30;
//			messageInput.y = SP.height - 50;
//			messageSendButton.x = messageInput.x + messageInput.width + 10;
//			messageSendButton.y = messageInput.y;
//			messageOutput.x = 30;
//			messageOutput.y = messageInput.y - messageOutput.height - 10;

			messagePanel.y = SP.height - messagePanel.height - 20;
			messagePanel.x = 30;
		}

	}
}
